//
//  AIInfoChatView.m
//  Mia
//
//  Created by Vill on 2023/12/25.
//

#import "AIInfoChatView.h"
#import "AIInfoMsgViewCell.h"

@interface AIInfoChatView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *messages;
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, assign) NSInteger currentCharacterIndex;
@end

@implementation AIInfoChatView

- (instancetype)initWithFrame:(CGRect)frame{
    if(self == [super initWithFrame:frame]){
        [self setUI];
    }
    return self;
}

-(void)setUI{
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    // 初始化聊天数据
    self.messages = @[@"HI，I'm Jack",@"I am good at tarot cards and horoscopes, you can tell me more information about yourself, I can help you better."];
    // 开始逐字显示消息内容
    self.currentIndex = 0;
    self.currentCharacterIndex = 0;
    [self showNextCharacter];
}

- (void)showNextCharacter {
    if (self.currentIndex >= self.messages.count) {
        return;
    }
    NSString *message = self.messages[self.currentIndex];
    if (self.currentCharacterIndex < message.length) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:self.currentIndex];
        AIInfoMsgViewCell *cell = (AIInfoMsgViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
        // 逐字显示消息内容
        NSString *displayMessage = [message substringToIndex:self.currentCharacterIndex + 1];
        [cell setCharacters:displayMessage];
        self.currentCharacterIndex++;
        // 延迟0.1秒递归调用，实现逐字显示效果
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.06 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self showNextCharacter];
        });
    } else {
        self.currentIndex++;
        self.currentCharacterIndex = 0;
        // 显示完一组消息后，延迟1秒显示下一组消息
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self showNextCharacter];
        });
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.messages.count;
}

// 实现UITableViewDataSource协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"ChatCell";
    AIInfoMsgViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[AIInfoMsgViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return PTW(15);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, PTW(20))];
    v.backgroundColor = UIColor.clearColor;
    return v;
}

#pragma mark ------------------ lazy --------------
- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;/// 高度自适应
        // 注册自定义的cell类
        [_tableView registerClass:[AIInfoMsgViewCell class] forCellReuseIdentifier:@"ChatCell"];
    }
    return _tableView;
}

@end
